
import axios from "axios";
import qs from "qs";
import { message } from 'ant-design-vue';
import Storage from "good-storage";
import { openApiSign } from './settings'
import Vue from 'vue'
import i18n from "@/plugins/i18n";

const lang = 'ch';
const service = axios.create({
  //baseURL: process.env.baseUrl, // url = base url + request url
  timeout: 5000 // request timeout
})
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
let loadingBox = '';
// request拦截器
service.interceptors.request.use(config => {
  if (config.loading) {
    const hide = message.loading('加载中...', 0);
    setTimeout(hide, 2500);
  }

  if (process.env.node_env == 'production' || process.env.node_env == 'uat'||process.env.node_env == 'productionus') {
    config.baseURL = process.env.baseUrl
  }
  
  console.log(process.env.node_env, process.env.baseUrl, config.baseURL, 'process.env.node_env')
  const isToken = (config.headers || {}).isToken === true
  if (isToken) {
    config.headers['Authorization'] = 'Bearer ' + Storage.get('TOKEN') // 让每个请求携带自定义token 请根据实际情况自行修改
    config.headers['token'] = Storage.get('TOKEN')
  }
  if (config.method === 'get' && config.data) {
    let url = config.url + '?openApiSign=' + process.env.openApiSign + (config.data ? ('&' + qs.stringify(config.data)) : '')
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  } else {
    config.url = config.url;
  }
  return config
}, error => {
  console.log(error)
  Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(res => {
  if (loadingBox) {
    loadingBox.close();
  }
  // 未设置状态码则默认成功状态 errorCode
  // 获取错误信息
  const msg = '程序异常,请联系管理员'
  const code = res.data.code;
  const errmsg = res.data.errmsg || res.data.msg;
  const errorCode = res.data.errorCode;
  let sysMessage = res.data;
  if (errorCode) {
    message.error(res.data.message || sysMessage)
    return Promise.reject(new Error(sysMessage))
  }
  if (code === 401) {
    message.error(res.data.msg || '无效的会话，或者会话已过期，请重新登录。')
    Storage.set('TOKEN','')
    setTimeout(() => {
     // location.href = './login';
    }, 1500);
    return Promise.reject(res.data.msg || '无效的会话，或者会话已过期，请重新登录。')
  } else if (code === 500) {
    //subCode
    if (!res.data.subCode) {
      message.error(sysMessage);
    }
    //console.log(Vue.$t('overseas.subCode.500-1'), '222222222222~~~~~~~~~')

    return Promise.reject(res.data)
  } else if (code !== 200) {
    message.error(errmsg)
    return Promise.reject(sysMessage)
  } else {
    return res.data
  }
},
  error => {
    if (loadingBox) {
      loadingBox.close();
    }
    console.log('err' + error)
    let msg = error.message;
    if (msg == "Network Error") {
      msg = "后端接口连接异常，";
      message.error(msg)
      setTimeout(() => location.reload(), 5 * 1000);
    } else if (msg.includes("timeout")) {
      msg = "系统接口请求超时";
    } else if (msg.includes("Request failed with status code")) {
      msg = "系统接口" + msg.substr(msg.length - 3) + "异常";
    }
    message.error(msg)
    return Promise.reject(error)
  }
)
export default service;
